草庐IT

javascript oop、instanceof 和基类

全部标签

没有动态调度开销的基类抽象方法的 C++ 习惯用法?

在C++中,是否有任何方法可以在不将方法声明为的情况下拥有“抽象”基类方法(即,从基类声明和调用,但在子类中实现)虚拟?当然,这个问题只适用于不需要多态性的情况(从未使用过的基类型的指针/引用)。请考虑以下事项:#defineNO_OPTasmvolatile("");//topreventsomecompileroptimizationtemplatevoiddoSomething(DerivedType&d){d.foo();}namespacetest1{structBase{inlinevoidfoo(){//...docommonstuffpre-call...foo_imp

c++ - 使用 std::enable_if 和可变参数基类

这是我正在尝试做的一个简化示例:#include#include#includetemplateclassfoo{public:templatetypenamestd::enable_if::value>::typebar(constU&t){std::coutclassbaz:publicfoo...{};intmain(){bazb;b.bar(1.0);}这给了我模棱两可的函数错误:error:requestformember'bar'isambiguousb.bar(1.0);note:candidatesare:templatetypenamestd::enable_if::

c++ - 自动实现调用特定基类构造函数的构造函数

classA{A(inta);};classB:publicA{usingA::A;//ShorthandforB(intb):A(b){}?};intmain(){Bb(3);return0;}有没有什么方法可以完成上述程序的目的(让B有一个与基类具有相同参数的构造函数)?这是它的正确语法吗?如果是这样,它是C++11/14的功能,还是可以在C++03中完成? 最佳答案 Istheresomewaytoaccomplishwhattheaboveprogramseeksto(tomakeBhaveaconstructorwitht

c++ - C++ 中是否有一种机制可以在没有动态内存分配的情况下从基类指针复制派生类的完整拷贝?

考虑以下示例,其中对象切片发生在基指针的取消引用期间。#includeclassBase{public:virtualvoidhello(){printf("helloworldfrombase\n");}};classDerived:publicBase{public:virtualvoidhello(){printf("helloworldfromderived\n");}};intmain(){Base*ptrToDerived=newDerived;autod=*ptrToDerived;d.hello();}我希望变量d保存类型为Derived的对象,而不是类型为Base的对

c++ - 通用基类合法吗?

这似乎可行,但我不能100%确定它是否合法,希望得到一些反馈。我有一个从通用基类派生的子类。它类似于奇怪的重复出现的模板模式,但又有所不同。在derived.h中:templateclassDerived:publicT{public:Derived();...andsomeotherstuff...};在derived.cpp中:#include"derived.h"templateDerived::Derived(){...}//definingthevariationsIneedhereavoidslinkererrors//seehttp://www.parashift.com

c++ - c++调用基类的ostream友元函数

所以,我有两个类:classBase{private:intnumber;public:friendostream&operator我的问题是,如何从子类中调用基类的友元函数输出其内容:output提前致谢:) 最佳答案 output(n) 关于c++-c++调用基类的ostream友元函数,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/10367271/

C++ - 基类和私有(private)头文件

我正在用C++编写一个库,并且有一个像这样的类层次结构:message.h文件(在./mylib/src中)classMessage{};request.h文件(在./mylib/include/mylib中)#include"message.h"classRequest:publicMessage{};response.h文件(在./mylib/include/mylib中)#include"message.h"classResponse:publicMessage{};我希望对用户隐藏mylib/src文件夹中的所有内容,并且只想分发mylib/include中的文件。但问题是re

c++ - 基类中的私有(private)成员和公共(public)成员有什么区别?

#pragmapack(push,4)classFather{public:intb;charc;};classChild:publicFather{chare;};#pragmapack(pop)sizeof(父亲)=8sizeof(child)=12但是如果我们像这样改变父类:classFather{private://changefrompublicintb;charc;};sizeof(child)=8 最佳答案 它是编译器的一个实现细节。换句话说,这不是你的事,除非你真的、真的需要让你的数据尽可能小。请注意此处的过早优化。

c++ - swig 对基类 'std::string' 一无所知,忽略

我正在尝试使用swig围绕C++库构建ruby​​包装器。其中大部分似乎都有效,但我有一个问题,我很确定与上述警告有关。看起来我正在包装的类之一是从std::string继承的。我在运行swig时看到上面的警告消息。当我在应该返回字符串的ruby​​对象上调用方法时,我看到了这个SWIG::Type_p_std__string:0x.....我在想我需要解决上面的警告,让它起作用,有什么想法吗? 最佳答案 SWIG提示它不知道std::string类,因此无法为其生成代码。SWIG库std_string.i具有用于将C++字符串映射

c++ - 我可以使用类级别的 typedef 作为基类的模板参数吗?

假设一个模板化的基类:templateclassBaseClass;在其他类中,我想从中继承,其中T是一个相当复杂的类型,所以我想使用typedef。因为我不想污染命名空间,所以我希望在类定义中包含typedef:classChildClass:publicBaseClass{typedefMyVeryVeryComplicatedTypeLocalType;...}现在当然我还不能使用LocalType作为BaseClass的模板参数并且必须写两次复杂的定义(MyVeryVeryComplicatedType)。(所以我猜标题问题的答案是“否”。)问题:有没有办法只定义一次,但仍然只